package com.wgn.reportquery.serlet;

//导入必需的 java 库
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.wgn.reportquery.cons.Const;
import com.wgn.reportquery.dao.ImportDaoImple;
import com.wgn.reportquery.http.QueryHttpClient;
import com.wgn.reportquery.model.Query;
import com.wgn.reportquery.model.ReportDate;
import com.wgn.reportquery.model.Reportdata;
import com.wgn.reportquery.rj.model.RJReportInfo;
import com.wgn.reportquery.service.ImportService;
import com.wgn.reportquery.service.QueryService;
import com.wgn.reportquery.service.SignService;
import com.wgn.reportquery.thread.ImportSkThread;
import com.wgn.reportquery.utils.Importer;
import com.wgn.reportquery.utils.ReportUtils;

//扩展 HttpServlet 类
public class SigninServlet extends HttpServlet {

	private static final long serialVersionUID = 3L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// String reportHtml = "";
		String getMoreHtml = "";
		String outinfo = "";

		HttpSession session = request.getSession(false);
		if (null != session) {
			try {
				String userName = (String) (session
						.getAttribute(Const.userName));
				String passwd = (String) (session.getAttribute(Const.passwd));
				String type = request.getParameter("type");
				if (type != null) {
					if (type.equalsIgnoreCase("code")) {
						String random = request.getParameter("id");

						QueryHttpClient skq = new QueryHttpClient(userName);
						request.getSession().setAttribute("skq", skq);
						String wholeurl = "http://yuyue.shdc.org.cn/verifycode.xujie?id="
								+ random;
						response.setContentType("image/*"); // 设置返回的文件类型
						InputStream in = skq.getImgSk(wholeurl);

						OutputStream o = response.getOutputStream();
						int l = 0;
						byte[] buffer = new byte[4096];
						while ((l = in.read(buffer)) != -1) {
							o.write(buffer, 0, l);
						}
						o.flush();
						in.close();
						o.close();
						return;
					}
				}
				if (null != userName && null != passwd) {// 已经登陆
					outinfo = "欢迎" + userName;
					// reportHtml = ss.getReport4Short(userName);

				} else {// 登陆
					userName = request.getParameter("username");
					passwd = request.getParameter("passwd");
					if (null != userName && null != passwd) {// 登陆
						SignService ss = new SignService();
						ss.getSignResult(userName, passwd, session);
						outinfo = "欢迎" + userName;
						// reportHtml = ss.getReport4Short(userName);

					} else {
						String passwd1 = request.getParameter("passwd1");

						if (null != passwd1)// 注册
						{
							SignService ss = new SignService();

							try {
								outinfo = ss.getRegResult(userName, passwd1,
										session);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								request.setAttribute("err", e.getMessage());
								getServletContext().getRequestDispatcher(
										"/jsp/regist.jsp").forward(request,
										response);
								return;
							}

						} else// 脱机
						{
							getServletContext().getRequestDispatcher(
									"/jsp/signin.jsp").forward(request,
									response);
							return;
						}
					}
				}// 201766首页增加当日报告单
				SignService ss = new SignService();
				List<String> khs = ss.getRjCards(userName);
				if (null != khs && khs.size() > 0) {
					 ReportDate date = ReportUtils.getNextPreDate();
//					ReportDate date = new ReportDate();
//					date.setEdn("20170524");
//					date.setStart("20170522");
					StringBuilder s = new StringBuilder();
					for (String kh : khs) {
						List<RJReportInfo> reportlist = ss.getReport(userName,
								date.getStart(), date.getEdn(), kh);
						String reportHtml = ReportUtils
								.getRJQuerListHtml(reportlist);
						s.append(reportHtml);
					}

					request.setAttribute("reportHtml", s.toString());
				}

				// 跳转
				request.setAttribute("userName", userName);
				// request.setAttribute("reportHtml", reportHtml);
				request.setAttribute("getMoreHtml", getMoreHtml);
				request.setAttribute("outinfo", outinfo);
				getServletContext().getRequestDispatcher("/jsp/index.jsp")
						.forward(request, response);
			} catch (Exception e) {
				request.setAttribute("err", e.getMessage());
				getServletContext().getRequestDispatcher("/jsp/signin.jsp")
						.forward(request, response);

			}

		} else {

			getServletContext().getRequestDispatcher("/jsp/signin.jsp")
					.forward(request, response);
		}

	}

	/**
	 * 
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}
}